﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using DoNetDrive.Common.Extensions;
using System.Linq;
namespace DoNetTool.Common
{
    /// <summary>
    /// 参数验证工具类
    /// </summary>
    public class ValidateTool
    {
        /// <summary>
        /// 初始化参数集合
        /// </summary>
        /// <returns></returns>
        public static Dictionary<ValidateValue, Func<string, string, string, string>> InitParamterDict()
        {
            return new Dictionary<ValidateValue, Func<string, string, string, string>>();
        }

        /// <summary>
        /// 生成带有签名的字典对象
        /// </summary>
        /// <param name="oParDetail">类对象</param>
        /// <param name="AppSecret">密钥</param>
        /// <param name="AppKey">键值</param>
        /// <returns></returns>
        public static SortedDictionary<string, string> CreateSignature(object oParDetail, string AppSecret, string AppKey)
        {
            string sJson = oParDetail.ToJSON();
            Dictionary<string, object> oDic = sJson.FromJson<Dictionary<string, object>>();
            
            SortedDictionary<string, string> par = new SortedDictionary<string, string>();
            foreach (KeyValuePair<string, object> item in oDic)
            {
                if (item.Value is System.Collections.ArrayList)
                {
                    continue;
                }
                if (item.Key == "Signature")
                {
                    continue;
                }
                if (item.Value != null && !string.IsNullOrEmpty(item.Value.ToString()))
                {
                    par.Add(item.Key, item.Value.ToString());
                }
            }

            par.Add("Signature", SignatureTool.CreateSignatureHash(par, AppSecret, AppKey));
            return par;
        }

        /// <summary>
        /// 验证带有签名属性对象参数
        /// </summary>
        /// <param name="parameter">SignatureBase 继承类</param>
        /// <param name="AppSecret">密钥</param>
        /// <param name="validateMessage">错误消息</param>
        /// <param name="FunList">自定义验证方法委托列表 string func(string,string,string)</param>
        /// <returns>成功或失败</returns>
        public static bool ValidateSignature(SignatureBase parameter, string AppSecret, ref string validateMessage, Dictionary<ValidateValue, Func<string, string, string, string>> FunList = null)
        {
            if (!parameter.ValidateTimestamp())
            {
                validateMessage = "10002 时间戳校验失败";
                return false;
            }

            if (FunList != null && FunList.Count > 0)
            {
                foreach (KeyValuePair<ValidateValue, Func<string, string, string, string>> func in FunList)
                {
                    validateMessage = func.Value(func.Key.Value, func.Key.CName, func.Key.RangeList);
                    if (validateMessage != "")
                    {
                        return false;
                    }
                }
            }
            
            if (string.IsNullOrWhiteSpace(AppSecret))
            {
                validateMessage = "10001 AppKey不存在";
                return false;
            }

            SortedDictionary<string, string> sdPar = CreateSignature(parameter, AppSecret, parameter.AppKey);
            string strSignature = sdPar["Signature"];
            if (strSignature != parameter.Signature)
            {
                validateMessage = "10000 签名校验失败";
                return false;
            }

            return true;
        }


        /// <summary>
        /// 接口参数验证
        /// </summary>
        /// <param name="FunList">自定义验证方法委托列表 string func(string,string,string)</param>
        /// <param name="validateMessage">错误消息</param>
        /// <returns></returns>
        public static bool ValidateParameter(Dictionary<ValidateValue, Func<string, string, string, string>> FunList, ref string validateMessage)
        {
            foreach (KeyValuePair<ValidateValue, Func<string, string, string, string>> func in FunList)
            {
                validateMessage = func.Value(func.Key.Value, func.Key.CName, func.Key.RangeList);
                if (validateMessage != "")
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// 验证参数不能为空
        /// </summary>
        /// <param name="paramValue">参数值</param>
        /// <param name="paramCName">参数名称</param>
        /// <param name="rangeList">没使用</param>
        /// <returns></returns>
        public static string ValidateNotNull(string paramValue, string paramCName, string rangeList = null)
        {
            string validateMessage = "";
            if (string.IsNullOrWhiteSpace(paramValue))
            {
                if (paramCName.Length > 2)
                {
                    validateMessage = paramCName.Substring(0, 2) + " " + paramCName.Substring(2) + "不能为空";
                }
                else
                {
                    validateMessage = "-1" + " " +  "";
                }
            }
            return validateMessage;
        }

        /// <summary>
        /// 验证参数必须的开头必须与指定的字符串匹配
        /// </summary>
        /// <param name="paramValue">参数值</param>
        /// <param name="paramCName">参数名称</param>
        /// <param name="rangeList">要比较的字符串</param>
        /// <returns></returns>
        public static string ValidateStartWith(string paramValue, string paramCName, string rangeList = null)
        {
            string validateMessage = "";
            if (!string.IsNullOrWhiteSpace(paramValue) && !string.IsNullOrWhiteSpace(rangeList))
            {
                if (!paramValue.StartsWith(rangeList))
                {
                    if (paramCName.Length > 2)
                    {
                        validateMessage = $"{paramCName.Substring(0, 2)} {paramCName.Substring(2)}开头必须与{rangeList}字符串匹配";
                    }
                    else
                    {
                        validateMessage = "-1" + " " + "";
                    }
                }
            }
            return validateMessage;
        }

        /// <summary>
        /// 验证参数必须的开头不能是指定的字符串
        /// </summary>
        /// <param name="paramValue">参数值</param>
        /// <param name="paramCName">参数名称</param>
        /// <param name="rangeList">要比较的字符串</param>
        /// <returns></returns>
        public static string ValidateNotNumberStartWith(string paramValue, string paramCName, string rangeList = null)
        {
            string validateMessage = "";
            if (!string.IsNullOrWhiteSpace(paramValue))
            {
                Regex r = new Regex("(?<num>[0-9]\\d*)[^\\s]*");
                Match match = r.Match(paramValue);
                if (match.Success)
                {
                    string numStr = match.Groups["num"].Value;
                    if (paramValue.StartsWith(numStr))
                    {
                        if (paramCName.Length > 2)
                        {
                            validateMessage = $"{paramCName.Substring(0, 2)} {paramCName.Substring(2)}不能是数字开头";
                        }
                        else
                        {
                            validateMessage = "-1" + " " + "";
                        }
                    }
                }
            }
            return validateMessage;
        
        }

        /// <summary>
        /// 验证Int32整数
        /// </summary>
        /// <param name="paramValue">参数值</param>
        /// <param name="paramCName">参数名称</param>
        /// <param name="rangeList"></param>
        /// <returns></returns>
        public static string ValidatePositiveInteger(string paramValue, string paramCName, string rangeList = null)
        {
            string validateMessage = "";
            if (!string.IsNullOrWhiteSpace(paramValue))
            {
                int iParse = 0;
                
                if (!int.TryParse(paramValue, out iParse))
                {
                    if (paramCName.Length > 2)
                        validateMessage = $"{paramCName.Substring(0, 2)} " + paramCName.Substring(2) + "不正确";
                    else
                        validateMessage = "-1" + " " + "";
                }
            }
            return validateMessage;
        }

        /// <summary>
        /// 验证Int16整数
        /// </summary>
        /// <param name="paramValue">参数值</param>
        /// <param name="paramCName">参数名称</param>
        /// <param name="rangeList"></param>
        /// <returns></returns>
        public static string ValidateByte(string paramValue, string paramCName, string rangeList = null)
        {
            string validateMessage = "";
            if (!string.IsNullOrWhiteSpace(paramValue))
            {
                byte bParse = 0;

                if (!byte.TryParse(paramValue, out bParse))
                {
                    if (paramCName.Length > 2)
                        validateMessage = $"{paramCName.Substring(0, 2)} " + paramCName.Substring(2) + "不正确";
                    else
                        validateMessage = "-1" + " " + "";
                }
            }
            return validateMessage;
        }

        /// <summary>
        /// 验证Int24整数
        /// </summary>
        /// <param name="paramValue">参数值</param>
        /// <param name="paramCName">参数名称</param>
        /// <param name="rangeList"></param>
        /// <returns></returns>
        public static string ValidateShort(string paramValue, string paramCName, string rangeList = null)
        {
            string validateMessage = "";
            if (!string.IsNullOrWhiteSpace(paramValue))
            {
                short sParse = 0;

                if (!short.TryParse(paramValue, out sParse))
                {
                    if (paramCName.Length > 2)
                        validateMessage = $"{paramCName.Substring(0, 2)} " + paramCName.Substring(2) + "不正确";
                    else
                        validateMessage = "-1" + " " + "";
                }
            }
            return validateMessage;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="paramValue"></param>
        /// <param name="paramCName"></param>
        /// <param name="rangeList"></param>
        /// <returns></returns>
        public static string ValidateCardData(string paramValue, string paramCName, string rangeList = null)
        {
            string validateMessage = "";
            if (!string.IsNullOrWhiteSpace(paramValue))
            {
                if (!IsCardData(paramValue))
                {
                    if (paramCName.Length > 2)
                        validateMessage = $"{paramCName.Substring(0, 2)} " + paramCName.Substring(2) + "不正确";
                    else
                        validateMessage = "-1" + " " + "";
                }
            }
            return validateMessage;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sValue"></param>
        /// <returns></returns>
        public static bool IsCardData(string sValue)
        {
            decimal dParse = 0;
            decimal maxCardData = 4722366482869645213695m;
            if (!decimal.TryParse(sValue, out dParse) || dParse > maxCardData)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 验证自然数
        /// </summary>
        /// <param name="paramValue"></param>
        /// <param name="paramCName"></param>
        /// <param name="rangeList"></param>
        /// <returns></returns>
        public static string ValidateNaturalNumber(string paramValue, string paramCName, string rangeList = null)
        {
            string validateMessage = "";
            if (!string.IsNullOrWhiteSpace(paramValue))
            {
                if (!IsNaturalNumber(paramValue))
                {
                    if (paramCName.Length > 2)
                        validateMessage = $"{paramCName.Substring(0, 2)} " + paramCName.Substring(2) + "不正确";
                    else
                        validateMessage = "-1" + " " + "";
                }
            }
            return validateMessage;
        }

        /// <summary>
        /// 是否是手机号
        /// </summary>
        /// <param name="paramValue"></param>
        /// <param name="paramCName"></param>
        /// <param name="rangeList"></param>
        /// <returns></returns>
        public static string ValidateIsPhone(string paramValue, string paramCName, string rangeList = null)
        {
            string validateMessage = "";
            if (!string.IsNullOrWhiteSpace(paramValue))
            {
                if (!IsPhone(paramValue))
                {
                    if (paramCName.Length > 2)
                        validateMessage = $"{paramCName.Substring(0, 2)} " + paramCName.Substring(2) + "不是手机号";
                    else
                        validateMessage = "-1" + " " + "";
                }
            }
            return validateMessage;
        }


        /// <summary>
        /// 是否是电子邮箱
        /// </summary>
        /// <param name="paramValue"></param>
        /// <param name="paramCName"></param>
        /// <param name="rangeList"></param>
        /// <returns></returns>
        public static string ValidateIsEmail(string paramValue, string paramCName, string rangeList = null)
        {
            string validateMessage = "";
            if (!string.IsNullOrWhiteSpace(paramValue))
            {
                if (!IsEmail(paramValue))
                {
                    if (paramCName.Length > 2)
                        validateMessage = $"{paramCName.Substring(0, 2)} " + paramCName.Substring(2) + "不是电子邮箱格式";
                    else
                        validateMessage = "-1" + " " + "";
                }
            }
            return validateMessage;
        }

        /// <summary>
        /// 验证参数日期格式
        /// </summary>
        /// <param name="paramValue">参数值</param>
        /// <param name="paramCName">参数名称</param>
        /// <param name="rangeList"></param>
        /// <returns></returns>
        public static string ValidateDateTime(string paramValue, string paramCName, string rangeList = null)
        {
            string validateMessage = "";
            if (string.IsNullOrWhiteSpace(paramValue))
            {
                validateMessage = $"{paramCName.Substring(0, 1)} " + paramCName + "不能为空";
            }
            else
            {
                DateTime dt = DateTime.Now;
                if (!DateTime.TryParse(paramValue, out dt))
                {
                    if (paramCName.Length > 2)
                        validateMessage = $"{paramCName.Substring(0, 2)} " + paramCName.Substring(2) + "不正确";
                    else
                        validateMessage = "-1" + " " + "";
                }
            }
            return validateMessage;
        }

        /// <summary>
        /// 验证参数值范围
        /// </summary>
        /// <param name="paramValue">参数值</param>
        /// <param name="paramCName">参数名称</param>
        /// <param name="rangeList">1,2,3,4</param>
        /// <returns></returns>
        public static string ValidateRange(string paramValue, string paramCName, string rangeList)
        {
            string validateMessage = "";
            if (!string.IsNullOrWhiteSpace(paramValue) && !string.IsNullOrWhiteSpace(rangeList))
            {
                rangeList = "," + rangeList + ",";
                if (!rangeList.Contains("," + paramValue + ","))
                {
                    if (paramCName.Length > 2)
                        validateMessage = $"{paramCName.Substring(0, 2)} " + paramCName.Substring(2) + "不正确";
                    else
                        validateMessage = "-1" + " " + "";
                }
            }
            return validateMessage;
        }

        /// <summary>
        /// 验证ID 集合每个元素的长度
        /// </summary>
        /// <param name="paramValue">参数值</param>
        /// <param name="paramCName">参数名称</param>
        /// <param name="maxLength">每个元素的长度</param>
        /// <returns></returns>
        public static string ValidateListItemLength(string paramValue, string paramCName, string maxLength)
        {
            string validateMessage = "";
            if (!string.IsNullOrWhiteSpace(paramValue) && !string.IsNullOrWhiteSpace(maxLength))
            {
                int iOut = 0;
                if (!int.TryParse(maxLength,out iOut))
                {
                    if (paramCName.Length > 2)
                        validateMessage = $"{paramCName.Substring(0, 2)} 参数maxLength不是正整数";
                    else
                        validateMessage = "-1" + " " + "";
                    return validateMessage;
                }
                var listSN = paramValue.Split(',');
                if (listSN.Length > 0)
                {
                    bool bLength16 = listSN.Any(t => t.Length > iOut);
                    if (bLength16)
                    {
                        if (paramCName.Length > 2)
                            validateMessage = $"{paramCName.Substring(0, 2)} " + paramCName.Substring(2) + "其中某些Item 长度超过" + maxLength + "字";
                        else
                            validateMessage = "-1" + " " + "";
                    }
                }
            }
            return validateMessage;
        }

        /// <summary>
        /// 验证ID 集合元素总数
        /// </summary>
        /// <param name="paramValue">参数值</param>
        /// <param name="paramCName">参数名称</param>
        /// <param name="count">集合元素总数</param>
        /// <returns></returns>
        public static string ValidateListItemCount(string paramValue, string paramCName, string count)
        {
            string validateMessage = "";
            if (!string.IsNullOrWhiteSpace(paramValue) && !string.IsNullOrWhiteSpace(count))
            {
                int iOut = 0;
                if (!int.TryParse(count, out iOut) || count.Contains("-"))
                {
                    if (paramCName.Length > 2)
                        return $"{paramCName.Substring(0, 2)} 参数maxLength不是正整数";
                    else
                        return "-1" + " " + "";
                }
                var listSN = paramValue.Split(',');
                if (listSN.Length > iOut)
                {
                    if (paramCName.Length > 2)
                        validateMessage = $"{paramCName.Substring(0, 2)} " + paramCName.Substring(2) + "其中Item 总数不能超过" + count + "个";
                    else
                        validateMessage = "-1" + " " + "";
                }
            }
            return validateMessage;
        }

        /// <summary>
        /// 验证ID 集合元素正整数
        /// </summary>
        /// <param name="paramValue">参数值</param>
        /// <param name="paramCName">参数名称</param>
        /// <param name="count">集合元素总数</param>
        /// <returns></returns>
        public static string ValidateListItemInteger(string paramValue, string paramCName, string count=null)
        {
            string validateMessage = "";
            if (!string.IsNullOrWhiteSpace(paramValue))
            {
               
                var listSN = paramValue.Split(',');
                if (listSN.Length > 0)
                {
                    bool b = listSN.Any(t => !IsPositiveInteger(t));
                    if (b)
                    {
                        if (paramCName.Length > 2)
                            validateMessage = $"{paramCName.Substring(0, 2)} " + paramCName.Substring(2) + "其中Item 不是正整数";
                        else
                            validateMessage = "-1" + " " + "";
                    }
                    
                }
            }
            return validateMessage;
        }

        /// <summary>
        /// 验证参数值长度
        /// </summary>
        /// <param name="Value">参数值</param>
        /// <param name="paramCName">参数名称</param>
        /// <param name="rangeList">2-10</param>
        /// <returns></returns>
        public static string ValidateLength(string Value, string paramCName, string rangeList)
        {
            string validateMessage = "";
            int iParamLength = 0;
            if (string.IsNullOrWhiteSpace(Value))
            {
                iParamLength = 0;
            }
            else
            {
                iParamLength = Value.Trim().Length;
            }
            if (!string.IsNullOrWhiteSpace(rangeList))
            {
                string[] array = rangeList.Split('-');
                int iStart = 0;
                int iEnd = 0;

                if (array.Length == 2 && int.TryParse(array[0], out iStart) && int.TryParse(array[1], out iEnd))
                {
                    if (iParamLength > iEnd || iParamLength < iStart)
                    {
                        if (paramCName.Length > 2)
                            validateMessage = $"{paramCName.Substring(0, 2)} {paramCName.Substring(2)}长度必须是{iStart.ToString()}到{iEnd.ToString()}";
                        else
                            validateMessage = "-1" + " " + "";
                    }
                }
                else
                {
                    if (paramCName.Length > 2)
                        validateMessage = $"{paramCName.Substring(0, 2)} 长度范围参数不正确";
                    else
                        validateMessage = "-1" + " " + "";
                }
            }
            return validateMessage;
        }

        /// <summary>
        /// 验证字符串长度范围，并返回再范围内的字符串
        /// </summary>
        /// <param name="Value"></param>
        /// <param name="rangeList"></param>
        /// <returns></returns>
        public static string ValidateLength(string Value,  string rangeList)
        {

            int iParamLength;
            if (string.IsNullOrWhiteSpace(Value))
            {
                iParamLength = 0;
            }
            else
            {
                iParamLength = Value.Trim().Length;
            }
            if (!string.IsNullOrWhiteSpace(rangeList))
            {
                string[] array = rangeList.Split('-');
                int iStart = 0;
                int iEnd = 0;

                if (array.Length == 2 && int.TryParse(array[0], out iStart) && int.TryParse(array[1], out iEnd))
                {
                    if (iParamLength > iEnd || iParamLength < iStart)
                    {
                        if(iParamLength > iEnd)
                        {
                            return Value.Substring(0, iEnd);
                        }
                        else
                        {
                            return string.Empty;
                        }
                    }
                }
            }
            return Value;
        }


        /// <summary>
        /// 是否包含特殊字符
        /// </summary>
        /// <param name="paramValue"></param>
        /// <param name="paramCName"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        public static string ValidateIllegalChar(string paramValue, string paramCName, string maxLength)
        {
            string validateMessage = "";
            if (string.IsNullOrWhiteSpace(paramValue))
            {
                return "";
            }
            char[] illegalCharList = { '\\', '/', ':', '*', '?', '"', '<', '>', '|' };
            foreach (var item in illegalCharList)
            {
                if (paramValue.IndexOf(item) != -1)
                {
                    if (paramCName.Length > 2)
                        validateMessage = $"{paramCName.Substring(0, 2)} " + paramCName.Substring(2) + " 不能包含以下特殊字符 “\\/:*?\"<>|”";
                    else
                        validateMessage = "-1" + " " + "";
                    return validateMessage;
                }
            }
            return "";
        }

        /// <summary>
        /// 是否是身份证号
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsIDCard(string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return false;
            }

            value = value.Trim();

            Regex r = new Regex("^\\d{17}[a-zA-Z0-9]$");

            return r.IsMatch(value);
        }

        /// <summary>
        /// 是否是电子邮箱
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsEmail(string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return false;
            }

            value = value.Trim();
            Regex r = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
            return r.IsMatch(value);
        }

        /// <summary>
        /// 是否是IP地址
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsIPAddress(string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return false;
            }

            value = value.Trim();

            Regex r = new Regex("^((([1-9]?\\d)|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))\\.){3}(([1-9]?\\d)|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))$");

            return r.IsMatch(value);
        }

        /// <summary>
        /// 是否是自然数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNaturalNumber(string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return false;
            }

            value = value.Trim();

            Regex r = new Regex("^\\d+$");

            return r.IsMatch(value);
        }

        /// <summary>
        /// 是否是手机号
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsPhone(string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return false;
            }

            value = value.Trim();

            Regex r = new Regex("^1(3\\d)|(4[056789])|5(012356789)|(7[01345678])|(8\\d)|(9[89])\\d{8}$");

            return r.IsMatch(value);
        }

        /// <summary>
        /// 是否是正整数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsPositiveInteger(string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return false;
            }

            value = value.Trim();

            Regex r = new Regex("^[1-9]\\d*$");

            return r.IsMatch(value);
        }

        /// <summary>
        /// 是否是正整数集
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsPositiveIntegerSet(string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return false;
            }

            value = value.Trim();

            Regex r = new Regex("^[1-9]\\d*(\\,[1-9]\\d*)*$");

            return r.IsMatch(value);
        }

        /// <summary>
        /// 是否是正数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsPositiveNumber(string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return false;
            }

            value = value.Trim();

            Regex r = new Regex("^[1-9]\\d*(\\.\\d+)*|0\\.0*[1-9]+$");

            return r.IsMatch(value);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="base64String"></param>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static bool IsBase64(string base64String, out byte[] bytes)
        {
            bytes = null;
            // Credit: oybek http://.com/users/794764/oybek
            if (string.IsNullOrEmpty(base64String) || base64String.Length % 4 != 0
               || base64String.Contains(" ") || base64String.Contains("\t") || base64String.Contains("\r") || base64String.Contains("\n"))
                return false;

            try
            {
                bytes = Convert.FromBase64String(base64String);
                return true;
            }
            catch (Exception)
            {
                // Handle the exception
            }

            return false;
        }


        /// <summary>
        /// 验证参数并返回正确值
        /// </summary>
        /// <param name="Value"></param>
        /// <param name="iMax"></param>
        /// <param name="iMin"></param>
        /// <param name="defValue"></param>
        public static int ValidateNumByDef( string Value, int iMax, int iMin, int defValue)
        {
            int retNum;
            if (!int.TryParse(Value,out retNum))
            {
                retNum = defValue;
            }


            if (!ValidateNum(retNum, iMax, iMin))
            {
                return defValue;
            }
            else
            {
                return retNum;
            }
        }

        /// <summary>
        /// 验证参数并返回正确值
        /// </summary>
        /// <param name="Value"></param>
        /// <param name="defValue"></param>
        public static string ValidateIPByDef(string Value,string defValue)
        {
            if (!IsIPAddress(Value))
            {
                return defValue;
            }
            else
            {
                return Value;
            }
        }


        /// <summary>
        /// 验证参数并返回正确值
        /// </summary>
        /// <param name="Value"></param>
        /// <param name="iMax"></param>
        /// <param name="iMin"></param>
        /// <param name="defValue"></param>
        public static int ValidateNumByDef(int Value,int iMax,int iMin ,int defValue)
        {
            if(!ValidateNum(Value, iMax,iMin))
            {
                return defValue;
            }
            else
            {
                return Value;
            }
        }

        /// <summary>
        /// 验证数值是否超出范围
        /// </summary>
        /// <param name="Value"></param>
        /// <param name="iMax"></param>
        /// <param name="iMin"></param>
        /// <returns></returns>
        public static bool ValidateNum(string Value, int iMax, int iMin)
        {
            int retNum;
            if (!int.TryParse(Value, out retNum)) return false;
            

            if (retNum > iMax) return false;
            if (retNum < iMin) return false;
            return true;
        }


        /// <summary>
        /// 验证数值是否超出范围
        /// </summary>
        /// <param name="Value"></param>
        /// <param name="iMax"></param>
        /// <param name="iMin"></param>
        /// <returns></returns>
        public static bool ValidateNum(int Value, int iMax, int iMin)
        {
            if (Value > iMax) return false;
            if (Value < iMin) return false;
            return true;
        }


    }
}
